package com.ls.fw.search.solrj.utils;

import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;

import com.ls.fw.net.httpclient.utils.HttpclientUtil;
import com.ls.fw.net.httpclient.utils.HttpclientUtil.HttpEntityHandler;


/**
 * solrj http工具类
 * @author ls
 *
 */
public class HttpUtil {

	private static final String WT_JSON = "?wt=json";
	public static final String COREADMINDOMAIN = "/admin/cores";
	public static final String COLLECTIONDOMAIN = "/admin/collections";
	public static final String CONFIGSDOMAIN = "/admin/configs";
	
	
	/**
	 * 单节点使用
	 * @param baseUrl <b>http://hostname:port/solr</b>
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public static String coreRequest(String baseUrl,Map<String,String> para) throws Exception{
		String t = HttpclientUtil.get(baseUrl+COREADMINDOMAIN+WT_JSON, para, new HttpEntityHandler<String>() {

			@Override
			public String handler(StatusLine arg0, HttpEntity arg1) throws Exception {
				return EntityUtils.toString(arg1);
			}
		});
		return t;
	}

	
	/**
	 * solrcloud 使用
	 * @param baseUrl <b>http://hostname:port/solr</b>
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public static String collectionRequest(String baseUrl,Map<String,String> para) throws Exception{
		String t = HttpclientUtil.get(baseUrl+COLLECTIONDOMAIN+WT_JSON, para, new HttpEntityHandler<String>() {

			@Override
			public String handler(StatusLine arg0, HttpEntity arg1) throws Exception {
				return EntityUtils.toString(arg1);
			}
		});
		return t;
	}
	
	/**
	 * This API can only be used with Solr running in SolrCloud mode
	 * @param baseUrl
	 * @param para
	 * @return
	 * @throws Exception
	 */
	public static String configsRequest(String baseUrl,Map<String,String> para) throws Exception{
		String t = HttpclientUtil.get(baseUrl+CONFIGSDOMAIN+WT_JSON, para, new HttpEntityHandler<String>() {

			@Override
			public String handler(StatusLine arg0, HttpEntity arg1) throws Exception {
				return EntityUtils.toString(arg1);
			}
		});
		return t;
	}
	
}
